// pages/setManager/setManager.js
let app = getApp()
let request = require('../../utils/request')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageAllHeight: app.globalData.pageAllHeight,
    managerId: 0,
    managerInfo: {},
    nameVal: '',
    phoneVal: '',
    parkId: 0,
    totalParkSpaceVal: '',
    reserveParkSpaceVal: '',
    chargingParkSpaceVal: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id ? options.id : 0
    this.setData({
      managerId: id,
      parkId: options.parkId,
      parkingInfo: {
        parkId: options.parkId,
        chargingParkSpace: options.chargingParkSpace,
        reserveParkSpace: options.reserveParkSpace,
        totalParkSpace: options.totalParkSpace
      },
      chargingParkSpaceVal: options.chargingParkSpace,
      reserveParkSpaceVal: options.reserveParkSpace,
      totalParkSpaceVal: options.totalParkSpace,
      managerInfo: {}
    })
    this.getManagerInfo()
  },
  /**
   * 获取管理员信息
   */
  getManagerInfo() {
    let that = this
    let id = this.data.managerId
    if (id == 0) {
      return
    }
    request.http('/park/park-manager/get-by-id', 'GET', { id: id })
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          that.setData({
            managerInfo: data.data,
            nameVal: data.data.realName,
            phoneVal: data.data.mobilePhone
          })
        }
      })
  },
  /**
   * 获取姓名
   */
  getNameVal(e) {
    let val = e.detail.value
    this.setData({
      nameVal: val
    })
  },
  /**
   * 获取泊位总数
   */
  getPhoneVal(e) {
    let val = e.detail.value
    if (val.length >= 11) {
      val = val.slice(0,11)
    }
    this.setData({
      phoneVal: val
    })
  },
  /**
   * 获取可约泊位
   */
  getTotalParkSpaceVal(e) {
    let val = e.detail.value
    this.setData({
      totalParkSpaceVal: val
    })
  },
  /**
   * 获取充电桩
   */
  getReserveParkSpaceVal(e) {
    let val = e.detail.value
    this.setData({
      reserveParkSpaceVal: val
    })
  },
  /**
   * 获取手机号
   */
  getChargingParkSpaceVal(e) {
    let val = e.detail.value
    this.setData({
      chargingParkSpaceVal: val
    })
  },
  /**
   * 设置用户信息
   */
  setManagerInfo() {
    let moblieReg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
    if (!moblieReg.test( this.data.phoneVal)) {
      wx.showToast({
        title: '请您填写正确手机号',
        icon: 'none'
      })
      return
    }
    if (this.data.managerId == 0) {
      this.addManagerInfo()
      return
    }
    let parmas = this.data.managerInfo
    parmas['realName'] = this.data.nameVal
    parmas['mobilePhone'] = this.data.phoneVal
    if (parmas.realName.length > 10) {
      wx.showToast({
        title: '管理员名称不能超过10位',
        icon: 'none'
      })
      return
    }
    request.http('/park/park-manager/update', 'POST', parmas, 0)
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          wx.showToast({ title: '修改成功' })
          wx.navigateBack({ delta: 1 })
        }
      })
  },
  /**
   * 添加用户信息
   */
  addManagerInfo() {
    let parmas = {
      realName: this.data.nameVal,
      mobilePhone: this.data.phoneVal,
      parkId: this.data.parkId
    }
    if (parmas.realName.length > 10) {
      wx.showToast({
        title: '管理员名称不能超过10位',
        icon: 'none'
      })
      return
    }
    if(!parmas.realName || !parmas.mobilePhone) {
      return
    }
    request.http('/park/park-manager/add', 'POST', parmas, 0)
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          wx.showToast({ title: '添加成功' })
          wx.navigateBack({ delta: 1 })
        }
      })
  },
  /**
   * 修改停车场信息
   */
  setParkingInfo() {
    let parmas = this.data.parkingInfo
    parmas['totalParkSpace'] = this.data.totalParkSpaceVal
    parmas['reserveParkSpace'] = this.data.reserveParkSpaceVal
    parmas['chargingParkSpace'] = this.data.chargingParkSpaceVal
    request.http('/park/park-info/audit-parkinfo-space', 'POST', parmas, 0)
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          wx.showToast({
            title: '修改成功',
          })
          // wx.navigateBack({
          //   delta: 1,
          // })
        }
      })
  }
})